package com.flowplan.ai.client;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 通义千问响应模型
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TongyiResponse {
    
    /**
     * 请求ID
     */
    private String id;
    
    /**
     * 对象类型
     */
    private String object;
    
    /**
     * 创建时间
     */
    private Long created;
    
    /**
     * 模型名称
     */
    private String model;
    
    /**
     * 选择列表
     */
    private List<Choice> choices;
    
    /**
     * 使用统计
     */
    private Usage usage;
    
    /**
     * 错误信息
     */
    private Error error;
    
    /**
     * 是否成功
     */
    public boolean isSuccess() {
        return error == null && choices != null && !choices.isEmpty();
    }
    
    /**
     * 获取生成的文本内容
     */
    public String getContent() {
        if (isSuccess()) {
            return choices.get(0).getMessage().getContent();
        }
        return null;
    }
    
    /**
     * 创建错误响应
     */
    public static TongyiResponse error(String message) {
        TongyiResponse response = new TongyiResponse();
        response.error = new Error();
        response.error.setMessage(message);
        return response;
    }
    
    /**
     * 选择结果
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Choice {
        private Integer index;
        private TongyiClient.ChatMessage message;
        private String finishReason;
    }
    
    /**
     * 使用统计
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Usage {
        private Integer promptTokens;
        private Integer completionTokens;
        private Integer totalTokens;
    }
    
    /**
     * 错误信息
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Error {
        private String code;
        private String message;
        private String type;
    }
}